﻿using System;
using System.Collections.Generic;

namespace RotovapPlatform.Domain.Common
{
	/// <summary>
	/// 操作结果基类
	/// </summary>
	public class OperationResult
	{
		/// <summary>
		/// 是否成功
		/// </summary>
		public bool Success { get; set; }

		/// <summary>
		/// 消息
		/// </summary>
		public string Message { get; set; }

		/// <summary>
		/// 创建成功结果
		/// </summary>
		/// <param name="message">成功消息</param>
		/// <returns>操作结果</returns>
		public static OperationResult Ok(string message = "操作成功")
		{
			return new OperationResult { Success = true, Message = message };
		}

		/// <summary>
		/// 创建失败结果
		/// </summary>
		/// <param name="message">错误消息</param>
		/// <returns>操作结果</returns>
		public static OperationResult Fail(string message = "操作失败")
		{
			return new OperationResult { Success = false, Message = message };
		}

		/// <summary>
		/// 创建异常结果
		/// </summary>
		/// <param name="ex">异常</param>
		/// <returns>操作结果</returns>
		public static OperationResult FromException(Exception ex)
		{
			return new OperationResult { Success = false, Message = ex.Message };
		}
	}

	/// <summary>
	/// 带返回值的操作结果
	/// </summary>
	/// <typeparam name="T">返回值类型</typeparam>
	public class OperationResult<T> : OperationResult
	{
		/// <summary>
		/// 返回值
		/// </summary>
		public T Data { get; set; }

		/// <summary>
		/// 创建成功结果
		/// </summary>
		/// <param name="data">返回值</param>
		/// <param name="message">成功消息</param>
		/// <returns>操作结果</returns>
		public static OperationResult<T> Ok(T data, string message = "操作成功")
		{
			return new OperationResult<T> { Success = true, Data = data, Message = message };
		}

		/// <summary>
		/// 创建失败结果
		/// </summary>
		/// <param name="message">错误消息</param>
		/// <returns>操作结果</returns>
		public new static OperationResult<T> Fail(string message = "操作失败")
		{
			return new OperationResult<T> { Success = false, Message = message };
		}

		/// <summary>
		/// 创建异常结果
		/// </summary>
		/// <param name="ex">异常</param>
		/// <returns>操作结果</returns>
		public new static OperationResult<T> FromException(Exception ex)
		{
			return new OperationResult<T> { Success = false, Message = ex.Message };
		}
	}
}